<?php
namespace plugins\encyclopedias\controller;

use app\common\lib\model_operate\Transit; //引入此类
use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Request;

/**
 * 
 */
class AdminIndexController extends PluginAdminBaseController
{
    private  $pageNum = "20";



    /**
     * 百科列表页面
     */
    public function index()
    {
        $data = $this->request->param();
        $where = 1;
        if( !empty($data['status']) ) $where .= ' and status ='.$data [ 'status' ];
        if( !empty($data['type']) ) $where .= " and type like '%".$data['type']."%'";
        if( !empty($data['title']) ) $where .= " and title like '%".$data['title']."%'";

        $sear['pagesize'] = isset( $data['pagesize'] ) ? $data [ 'pagesize' ] : $this->pageNum;
        $sear['type'] = isset( $data['type'] ) ? $data [ 'type' ] : '';
        $sear['title'] = isset( $data['title'] ) ? $data [ 'title' ] : '';
        $sear['status'] = isset( $data['status'] ) ? $data [ 'status' ] : '';

        $list = db('encyclopedias')->where($where)->paginates($sear['pagesize'] , false ,['query'=>$sear])->each(function ($item){

            $category = db('encyclopedias_category')->where('id',$item['category'])->find();
            if(!empty($category)){
                $item['category'] = $category['title'];
            }
            $item['time'] = date('Y-m-d H:i:s',$item['time']);
            $name = '-';
            if($item['userid'] == 0){
                $name = '管理员';
            }
            $item['username'] = $name;
            return $item;
        });

        $list->appends($data);
        $this->assign('list', $list);
        $this->assign('sear',$sear);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    /**
     * 删除百科
     */
    public function delEncy()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            return $this->error('请先选择要删除的列表！');
        }
        $res = Db::name('encyclopedias')->where('id', $data['id'])->delete();
        if($res){
            return $this->success('删除成功');
        }
        return $this->error('删除失败');
    }


    /**
     * 百科列表添加页面
     */
    public function addPage()
    {
        return $this->fetch();
    }

    /**
     * 百科列表编辑页面
     */
    public function editPage()
    {
        $param = $this->request->param();

        $info = db('encyclopedias')->where('id',$param['id'])->find();
        if(!empty($info['content'])){
            $info['content'] = htmlspecialchars_decode($info['content']);
        }
        $this->assign('id', $param['id']);
        $this->assign('info', $info);
        return $this->fetch();
    }



    /**
     * 添加百科
     */
    public function encyAdd()
    {
        $param = $this->request->param();
        $add['userid'] = 0;
        $add['time'] = time();
        $add['title'] = $param['title'];
        $add['content'] = $param['content'];
        $add['image'] = $param['image'];
        $add['category'] = $param['category'];

        $return = db('encyclopedias')->insert($add);
        if ($return) {
            return api_json_echo(true, '添加成功！', null, 200);
        } else {
            return api_json_echo(false, '添加失败！', null, 101);
        }
    }

    /**
     * 编辑百科
     */
    public function encyEdit()
    {
        $param = $this->request->param();
        $add['userid'] = 0;
        $add['time'] = time();
        $add['title'] = $param['title'];
        $add['content'] = $param['content'];
        $add['image'] = $param['image'];
        $add['category'] = $param['category'];

        $return = db('encyclopedias')->where('id',$param['id'])->update($add);
        if ($return) {
            return api_json_echo(true, '修改成功！', null, 200);
        } else {
            return api_json_echo(false, '修改失败！', null, 101);
        }
    }



    /**
     * 百科分类列表
     */
    public function categoryAllList()
    {
        $list = db('encyclopedias_category')->select();
        return api_json_echo( true , '获取成功' , $list , 200);

    }


    /**
     * 百科详情
     */
    public function encyInfo()
    {
        $param = $this->request->param();
        $info = db('encyclopedias')->where('id',$param['id'])->find();
        $full_image = '';
        if(!empty($info['image'])){
            $full_image = ZY_APP_PATH.$info['image'];
        }
        $info['full_image'] = $full_image;
        if(!empty($info['content'])){
            $info['content'] = htmlspecialchars_decode($info['content']);
        }
        return api_json_echo( true , '获取成功' , $info , 200);

    }


    /**
     * 百科分类列表
     */
    public function category()
    {
        $data = $this->request->param();
        $where = 1;
        $sear['pagesize'] = isset( $data['pagesize'] ) ? $data [ 'pagesize' ] : $this->pageNum;
        $list = db('encyclopedias_category')->where($where)->paginates($sear['pagesize'] , false ,['query'=>$sear])->each(function ($item){
            $item['imgurl'] = ZY_APP_PATH.$item['imgurl'];
            return $item;
        });
        $list->appends($data);
        $this->assign('list', $list);
        $this->assign('sear',$sear);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    /**
     * 百科分类添加页面
     */
    public function addCategoryPage()
    {
        return $this->fetch();
    }
    /**
     * 百科分类编辑页面
     */
    public function editCategoryPage()
    {
        $param = $this->request->param();
        $data  = Db::name('encyclopedias_category')->where('id', $param['id'])->find();
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * 添加分类
     */
    public function categoryAdd()
    {
        $param = $this->request->param();
        $add = $param['add'];
        $add['createtime'] = time();
        if(empty($add['title']))return api_json_echo(false,'标题为空！');
        $catname = db('encyclopedias_category')->where('title', $add['title'])->find();
        if ($catname) {
            return api_json_echo(false, '该分类已存在！', null, 100);
        }
        $return = db('encyclopedias_category')->insert($add);
        if ($return) {
            return api_json_echo(true, '添加成功！', null, 200);
        } else {
            return api_json_echo(false, '添加失败！', null, 101);
        }
    }

    /**
     * 编辑分类
     */
    public function editCategory()
    {
        $param = $this->request->post();
        $data = Db::name('encyclopedias_category')->where('id', $param['id'])->setField($param);
        return api_json_echo(true, '编辑成功', null, 200);
    }

    /**
     * 删除分类
     */
    public function delCategory()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            return $this->error('请先选择要删除的分类！');
        }
        $res = Db::name('encyclopedias_category')->where('id', $data['id'])->delete();
        if($res){
            return $this->success('删除成功');
        }
        return $this->error('删除失败');
    }

    /**
     * 分类排序
     */
    public function listorder()
    {
        $param = $this->request->param();
        $list  = array_combine($param['id'], $param['listorder']);
        if (isset($list)) {
            foreach ($list as $id => $listorder) {
                $save = db('encyclopedias_category')->where('id', $id)->setField('sort', $listorder);
            }

            $this->success('排序成功！');
        } else {
            $this->error('排序失败！');
        }
    }



    /**
     * 上传图片
     */
    public function upload()
    {
        $data        = $this->request->param();
        $file        = $_FILES['file'];
        $upload_path = "/upload/images";
        $res = uploadImg($file, $upload_path);
        if ($res['status'] != true) {
            return api_json_echo(false, '上传失败', $res['message'], $res['code']);
        }
        return api_json_echo(true, '上传成功', $res['data'], 200);
    }


}